package com.tql.mall.boss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tql.mall.boss.mapper.CategoryTwoMapper;
import com.tql.mall.boss.service.CategoryOneService;
import com.tql.mall.boss.service.CategoryThreeService;
import com.tql.mall.boss.service.CategoryTwoService;
import com.tql.mall.model.enums.category.OperFailEnum;
import com.tql.mall.model.product.BaseCategory2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service

public class CategoryTwoServiceImpl extends ServiceImpl<CategoryTwoMapper, BaseCategory2> implements CategoryTwoService{

    @Autowired
    private CategoryOneService categoryOneService;
    @Autowired
    private CategoryThreeService categoryThreeService;


    @Override
    public String saveLevelTwo(BaseCategory2 baseCategory2) {
        if (StringUtils.isEmpty(baseCategory2.getName()))
            return OperFailEnum.ADD_FAIL_NAME_NULL.getMsg();
        //上级编号不存在
        if (baseCategory2.getCategory1Id() == null || !categoryOneService.isExist(baseCategory2.getCategory1Id()))
            return OperFailEnum.ADD_FAIL_UPID_NULL.getMsg();
        baseMapper.insert(baseCategory2);
        return null;
    }


    @Override
    public boolean isExist(Long category2Id) {
        BaseCategory2 baseCategory2 = baseMapper.selectById(category2Id);
        if (baseCategory2 == null) return false;
        return true;
    }

    @Override
    public String deleteById(Long id) {
        BaseCategory2 baseCategory2 = baseMapper.selectById(id);
        if (baseCategory2 == null) return OperFailEnum.DELETE_FAIL_ID_NULL.getMsg();
        boolean isRely = categoryThreeService.isExistRely(id);
        if (isRely) return OperFailEnum.DELETE_FAIL_DOWNID_NONULL.getMsg();
        //执行删除
        baseMapper.deleteById(id);
        return null;
    }

    /**
     * 判断 二级项中是否存在依赖category1_id为 id的项
     * @param id
     * @return
     */
    @Override
    public boolean isExistRely(Long id) {
        QueryWrapper<BaseCategory2> baseCategory2QueryWrapper = new QueryWrapper<>();
        baseCategory2QueryWrapper.eq("category1_id",id);
        List<BaseCategory2> list = baseMapper.selectList(baseCategory2QueryWrapper);
        if (list == null || list.size() == 0) return false;
        return true;
    }

//    /**
//     * 根据一级分类中的id，获取List<BaseLevel>
//     * @param id
//     * @return
//     */
//    @Override
//    public List<BaseLevel> getBaseLevelList(Long id) {
//        List<BaseLevel> resList = new ArrayList<>();
//        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("category1_id",id);
//        List<BaseCategory2> list = baseMapper.selectList(queryWrapper);
//        for (BaseCategory2 baseCategory2:list){
//            BaseLevel tmpBaseLevel = new BaseLevel();
//            tmpBaseLevel.setCategoryId(baseCategory2.getId());
//            tmpBaseLevel.setCategoryName(baseCategory2.getName());
//            tmpBaseLevel.setCategoryChild(categoryThreeService.getBaseLevelList(baseCategory2.getId()));
//            resList.add(tmpBaseLevel);
//        }
//        return resList;
//    }

    @Override
    public String updateCategory(Long id, String newName, Long selectedCategory1Id) {
        BaseCategory2 baseCategory2 = baseMapper.selectById(id);
        if (baseCategory2.getName().equals(newName) && baseCategory2.getCategory1Id().equals(selectedCategory1Id)){//名字和上级都没改
            return null;
        }
        if (!baseCategory2.getCategory1Id().equals(selectedCategory1Id)){
            baseCategory2.setCategory1Id(selectedCategory1Id);
        }
        //判断是否存在重名
        //查看新的一级分类下的二级分类是否存在与newName重名的情况
        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category1_id",baseCategory2.getCategory1Id())
                        .eq("name",newName);
        BaseCategory2 category2 = baseMapper.selectOne(queryWrapper);
        if (category2 != null){ //有重名的情况
            return OperFailEnum.EDIT_FILE_NAME_DEPLICATED.getMsg();
        }
        baseCategory2.setName(newName);
        //执行更新
        baseCategory2.setUpdateTime(new Date());
        baseMapper.updateById(baseCategory2);
        return null;
    }
    @Override
    public List<BaseCategory2> getCategoryListWithSameParent(Long id) {
        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category1_id",id);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BaseCategory2> getCategoryListWithSameCategory1Id(Long id) {
        BaseCategory2 baseCategory2 = baseMapper.selectById(id);
        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category1_id",baseCategory2.getCategory1Id());
        return baseMapper.selectList(queryWrapper);
    }
}
